#include <base/Mem.h>
#include <cstring>
#include "base/Types.h"

T_DbsGetAllRecordAck* g_ptDbsGetAllRecordAck;

extern bool GetTableTotalRecordLen(WORD32* len);
extern bool UsfDbs_GetTableAllRecord(int id, int len, T_DbsGetAllRecordAck* ack);

bool DbsAccess_GetS1Association(P_DBS_GetS1Association_REQ* req, P_DBS_GetS1Association_ACK* ack)
{
    WORD32  dwLoop            = 0;
    WORD32  dwTotalRecordLen  = 0;
    T_LTE_NEINFORecord *ptNeInfo = NULL;

    if (!GetTableTotalRecordLen(&dwTotalRecordLen))
    {
        return false;
    }

    g_ptDbsGetAllRecordAck = (T_DbsGetAllRecordAck *)getUB();

    if (NULL == g_ptDbsGetAllRecordAck)
    {
        return false;
    }

    memset(g_ptDbsGetAllRecordAck, 0, dwTotalRecordLen + sizeof(T_DbsGetAllRecordAck));
    if (!UsfDbs_GetTableAllRecord(1, dwTotalRecordLen, g_ptDbsGetAllRecordAck))
    {
        retUB(g_ptDbsGetAllRecordAck);
        return false;
    }

    ptNeInfo = (T_LTE_NEINFORecord *)(g_ptDbsGetAllRecordAck->acRecord);
    for (dwLoop = 0 ; (dwLoop < g_ptDbsGetAllRecordAck->dwRecordNum) && (dwLoop < 100); dwLoop++)
    {
        if (1 == ptNeInfo->dwNeType)
        {
            if (g_ptDbsGetAllRecordAck->ucS1AssoNum >= 10)
            {
            	ack->ucS1AssoNum = g_ptDbsGetAllRecordAck->ucS1AssoNum;

                return false;
            }

            ack->awS1AssoID[ack->ucS1AssoNum] = ptNeInfo->wAssociationID;
            ack->ucS1AssoNum++;
        }

        ptNeInfo++;
    }

    ack->dwResult = 0;
    retUB(g_ptDbsGetAllRecordAck);
    return true;
}
